package com.ews.onlineexchange.service;

import java.util.Calendar;
import java.util.Date;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.query.Query;

import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.RejectRecord;
import com.ews.onlineexchange.model.ServiceSupplier;

@Named
@Stateless
public class ResendSms {

	
	@Inject
	EntityManager entityManager;
	@Inject 
	SmsService smsService;
	@Transactional
	public void resendSMS() {
//		for(int i = 0;i<10000;i++) {
			smsService.sendFailedSms();
//		}
		
	}
	
	@Transactional
	public void sendTestSMS() {
//		for(int i = 0;i<10000;i++) {
//			smsService.sendTestSms();
//		}
		
	}
	
	private String SUPPLIER_LIST  = " SELECT * FROM SERVICESUPPLIER  WHERE BUSINESSLICENSEIMAGE_ID IS NOT NULL ";
	private String SUPPLIER_IMAGE = " SELECT * FROM SERVICESUPPLIER_IMAGE WHERE SERVICESUPPLIER_ID = ? ";
	@Transactional
	public void updateSupplierBussinessImage() {
		//List<ServiceSupplier> suppliers = (List<ServiceSupplier>) entityManager.createNativeQuery(SUPPLIER_LIST);
		Session session = (Session) entityManager.getDelegate();
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createNativeQuery(SUPPLIER_LIST,ServiceSupplier.class);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
		while (sr.next()) {
			ServiceSupplier supplier = (ServiceSupplier) sr.get(0);
			
			
			
		}
		session.flush();
	}
	
	private static final String QUERY_ALL_VALID_CERT_EQL = ""
			+ "	SELECT			cert"
			+ "	FROM			QualificationCert cert "
			+ "	WHERE			cert.valid = true 	";
	
	
	public void checkCert() {
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
//		if (cal.get(Calendar.HOUR_OF_DAY) - 2 != 0)
//			return;
		
		Session session = (Session) entityManager.getDelegate();
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_VALID_CERT_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
		long count = 0L;
		
		while (sr.next()) {
			QualificationCert cert = (QualificationCert) sr.get(0);
			if(null == cert.getAllotedDate()) continue;
			if (cert.getAllotedDate().before(now)) {
				cert.setValid(false);
				cert.setAuditResult(false);
				RejectRecord rejectRecord = new RejectRecord();
				rejectRecord.setReason("资质有效期已过!");
				cert.setCurrRejectRecord(rejectRecord);
				cert.getHistoryRejectRecords().add(rejectRecord);
				session.update(cert);
			}
			count++;
			if (count % 200 == 0) {
				session.flush();
			}
		}
		session.flush();
		
	}
	
	
	private static final String QUERY_ALL_VALID_SUPPLIER_EQL = ""
			+ "	SELECT			ss"
			+ "	FROM			ServiceSupplier ss "
			+ "	WHERE			ss.valid = true "
			+ "	AND				ss.busnissAllotedTime IS NOT NULL";
	
	
	public void checkSupplier() {
		Date now = new Date();
		Calendar nowCal = Calendar.getInstance();
//		if (nowCal.get(Calendar.HOUR_OF_DAY) - 1 != 0)
//			return;
		
		Session session = (Session) entityManager.getDelegate();
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_VALID_SUPPLIER_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
		long count = 0L;
		
		while (sr.next()) {
			ServiceSupplier serviceSupplier = (ServiceSupplier) sr.get(0);
			if (serviceSupplier.getBusnissAllotedTime().before(now)) {
				RejectRecord rejectRecord = new RejectRecord();
				rejectRecord.setReason("营业期限到期!");
				serviceSupplier.setCurrRejectRecord(rejectRecord);
				serviceSupplier.getHistoryRejectRecords().add(rejectRecord);
				serviceSupplier.setValid(false);
				serviceSupplier.setAuditted(false);
				session.update(serviceSupplier);
			}
			
			count++;
			if (count % 200 == 0) {
				session.flush();
			}
		}
		session.flush();
		
	}
	
}
